package script;

import vo.*;
import tools.*;
import org.testng.annotations.Test;


import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.FormatFlagsConversionMismatchException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.sound.midi.Soundbank;

import org.openqa.selenium.By;
import org.openqa.selenium.By.ByLinkText;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;




public class Login {

		LoginPage loginpage=new LoginPage();
		webdriver_do wdo=new webdriver_do();
	
		  @BeforeMethod
		  public void beforeMethod() throws IOException {
			  wdo.init_webdriver();

			  System.out.println("login---webdriver成功！--");

			 // System.out.println("初始化---WD--成功");


  }
  
  
	



		  @DataProvider(name = "login_data")
		  public Iterator<Object[]> dataProvider() {
			  Set<Object[]> set = new HashSet<Object[]>();
			  // 通过添加不同数据可以对一个测试点进行一组测试用例的测试

		  //set.add(new String[] { "jamesz2016", "james9437974", "false" });
			  set.add(new String[] { "jamesz20111", "james9437974", "true" });

			  return set.iterator();
		  }
  
  
		  @Test(dataProvider="login_data")
		  public void login(String username, String password, String expected)  throws InterruptedException {

			 wdo.wd.get(loginpage.address);
			 wdo.wd.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
			 wdo.wd.findElement(By.linkText(loginpage.login_link)).click();

			 System.out.println("即将跳转到---登录页面");
			 wdo.wd.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
			 wdo.wd.findElement(By.name(loginpage.username)).clear();
			 wdo.wd.findElement(By.name(loginpage.username)).sendKeys("jamesz2011@126.com");
			//wdo.wd.findElement(By.xpath(loginpage.username1)).sendKeys("jamesz2011@126.com");

			 //wdo.wd.findElement(By.name(loginpage.username)).sendKeys(username);



			 wdo.wd.findElement(By.name(loginpage.password)).clear();
			wdo.wd.findElement(By.name(loginpage.password)).sendKeys("james9437974");


			//wdo.wd.findElement(By.name(loginpage.password)).sendKeys(password);



			 //wdo.wd.findElement(By.id(loginpage.password)).submit();

			 //wdo.wd.findElement(By.name(loginpage.login_but)).click();


			 WebElement but=wdo.wd.findElement(By.name(loginpage.login_but));
			 but.click();


			// boolean but_tetx=but.isEnabled();

			// System.out.println("login_button:"+but_tetx);

			/*
			WebElement img_test=wdo.wd.findElement(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/a/img"));



			boolean img_flag  =img_test.isDisplayed();

			String flag_img="false";


			System.out.println("img_flag:"+img_flag);

				//if(webdriver_do.isPresentEle(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/a/img"), (webdriver_do) wdo.wd)) {

					//if(isPresentEle(By.id("kw"), wd)) {
					//System.out.println("找到元素");

				if (flag_img.equals(flag_img)) {

				*/

			//img_test.click();
			 //String result =wdo.wd.findElement(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/ul/li[1]/a")).getAttribute("text");

			boolean result =wdo.wd.findElement(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/ul/li[1]/a")).getAttribute("text").contains(username);

			   System.out.println("username:"+result);


			 if (expected.equals("true")) {
				   //assert result;
				 System.out.println("login success");
				}
			 else {
				  //assert !result;
				  wdo.wd.quit();
					System.out.println("login error");


				}




			// System.out.println("but:"+but.getAttribute("value"));
			// Assert.assertEquals(but.getAttribute("value"), "selenium");
			 System.out.println("WebElement对象");

			 Thread.sleep(1000);


		//}



				/*else {
					System.out.println("登陆失败，quit");

					wdo.wd.quit();
				}*/





		  }


		  @AfterMethod
		  public void afterMethod() throws InterruptedException, UnsupportedEncodingException {

			  //Thread.sleep(5000);

			  wdo.wd.navigate().refresh();



			  WebElement img_button=wdo.wd.findElement(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/a/img"));



			  img_button.click();

			  //  wdo.wd.findElement(By.class("dropdown dropdown-avatar open"));
			  //wdo.wd.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
			  //img
			  //wdo.wd.findElement(By.linkText("dropdown")).click();
			  //logout

			  wdo.wd.findElement(By.linkText("退出")).click();

			  WebElement ok=wdo.wd.findElement(By.xpath("//*[@id=\"main\"]/div[1]"));
			  String ok_text= ok.getText();
			  System.out.println("ok_text:"+ok_text);


			  String s = "退出成功。";
			  byte[] bytes = s.getBytes();
			/*	String s1 = new String(bytes,"GBK"); */
			  String s1 = new String(bytes,"UTF-8");

			  String st = ok_text;
			  byte[] bytes1 = st.getBytes();
			/*	String s1 = new String(bytes,"GBK"); */
			  String s11 = new String(bytes1,"UTF-8");

			  System.out.println("s1:"+s1);
			  System.out.println("s11:"+s11);
			  //Assert.assertEquals(s11, s1);

		/*
			  if (s11.equals(s1)) {
				  System.out.println("找到---退出成功标志");
				  ok.click();
				  //Thread.sleep(10000);

				  System.out.println("ok\n");
				  System.out.println("测试完成");
			  }
			  else {
				  System.out.println("error\n");
				  System.out.println("测试error");
			  }

		*/
			  wdo.wd.quit();


		  }

		}
